package com.zhf.dao;

import com.zhf.entity.User;
import com.zhf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/9/27 15:17
 */
public class UserDao {

    //需求一: 编写一个注册用户的方法,接收的参数是一个User对象
    public int register(User user) throws SQLException {

        //获取QueryRunner对象
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

        //编写SQL
        String sql="insert into user values(?,?,?,?,?,?)";

        Object[] param={
                user.getUid(),
                user.getUsername(),
                user.getPassword(),
                user.getTelephone(),
                user.getBirthday(),
                user.getSex()
        };

        //执行插入操作
        int update = qr.update(sql, param);

        return update;
    }

    //需求二: 编写一个 用户登录的方法,接收的参数是 用户名 和密码, 返回值是User对象
    public User login(String username,String password) throws SQLException {
        //获取QueryRunner对象
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

        //编写SQL
        String sql="select * from user where username=? and password=?";

        //执行SQL语句
        User user = qr.query(sql, new BeanHandler<User>(User.class), username, password);

        return user;
    }
}
